package org.surfnet.oaaas.it;

import java.util.List;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.junit.Before;
import org.junit.Test;
import org.surfnet.oaaas.model.ResourceServer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ResourceServerTestIT extends AbstractAuthorizationServerTest {
	private WebResource webResource;

	@Before
	public void client() {
		ClientConfig config = new DefaultClientConfig();
		// Default jaxb provider cannot properly deserialize lists.
		config.getClasses().add(JacksonJsonProvider.class);

		webResource = Client.create(config).resource(baseUrl()).path("admin")
				.path("resourceServer");
		
		// /admin/resourceServer
	}

	@Test
	public void put() {
		ResourceServer resourceServer = buildResourceServer();

		final ClientResponse response = webResource.header("Authorization",
				authorizationBearer(ACCESS_TOKEN)).put(ClientResponse.class,
				resourceServer);

		assertEquals(201, response.getStatus());
		ResourceServer returnedResourceServer = response
				.getEntity(ResourceServer.class);
		assertEquals(resourceServer.getName(), returnedResourceServer.getName());
		assertNotNull("the server should generate an ID", returnedResourceServer.getId());
		assertNotNull("the server should generate a secret", returnedResourceServer.getSecret());
		System.out.println("ResourceServer:" + returnedResourceServer);
	}

//	@Test
//	public void putInvalid() {
//		ResourceServer resourceServer = buildResourceServer();
//
//		final ClientResponse response = webResource.header("Authorization",
//				authorizationBearer(ACCESS_TOKEN)).put(ClientResponse.class,
//				resourceServer);
//
//		assertEquals(201, response.getStatus());
//
//		final ClientResponse response2 = webResource.header("Authorization",
//				authorizationBearer(ACCESS_TOKEN)).put(ClientResponse.class,
//				resourceServer);
//		//System.out.println("--------->"+response2);
//		assertEquals(
//				"putting the same server twice should not work because id+name combination has unique constraint",
//				500, response2.getStatus());
//	}

//	@Test
//	public void get() {
//		// First get a non existing resource server
//		ClientResponse response = webResource.path("-1").header("Authorization", authorizationBearer(ACCESS_TOKEN))
//				.get(ClientResponse.class);
//		assertEquals("Random id should return nothing", 404, response.getStatus());
//
//		// Insert some random one.
//		ResourceServer existingResourceServer = putSomeResourceServer();
//
//		// Get it again.
//		final ResourceServer returnedFromGet = webResource.path(String.valueOf(existingResourceServer.getId()))
//				.header("Authorization", authorizationBearer(ACCESS_TOKEN)).get(ResourceServer.class);
//		assertEquals(existingResourceServer, returnedFromGet);
//
//		// Get all
//		final List<ResourceServer> returnFromGetAll = webResource.header("Authorization", authorizationBearer(ACCESS_TOKEN)).get(
//				new GenericType<List<ResourceServer>>() {});
//		assertTrue(returnFromGetAll.size() > 0);
//	}

	private ResourceServer putSomeResourceServer() {
		ResourceServer resourceServer = buildResourceServer();

		return webResource.header("Authorization", authorizationBearer(ACCESS_TOKEN)).put(ResourceServer.class,
				resourceServer);
	}
 
	private ResourceServer buildResourceServer() {
		ResourceServer resourceServer = new ResourceServer();
		resourceServer.setContactName("myContactName");
		resourceServer.setDescription("The description");
		resourceServer.setName("the name_" + System.currentTimeMillis());
		resourceServer.setKey("the-key-" + System.currentTimeMillis());
		resourceServer.setThumbNailUrl("http://example.com/thumbnail");
		return resourceServer;
	}
}
